package cn.com.klun.tjy.dao.impl;

import cn.com.klun.tjy.base.DieTaskOpenConfig;
import cn.com.klun.tjy.base.SiteChildTask;
import cn.com.klun.tjy.dao.TaskInfoDAO;
import cn.com.klun.tjy.dao.entity.TblAllStockInfo;
import cn.com.klun.tjy.dao.entity.TblTaskBasicConfig;
import cn.com.klun.util.BasisConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/** 
 * @ClassName: TaskInfoDAOImpl 
 * @Description: 查询任务信息 
 * @date 2015年1月20日 下午1:57:25
 */
@Service
public class TaskInfoDAOImpl implements TaskInfoDAO{
	
	private Logger logger = LoggerFactory.getLogger(this.getClass().getSimpleName());
	
	/**
	* @Title: getConn 
	* @Description: 获取数据库连接
	* @return Connection    返回类型 
	* @throws
	 */
	private Connection getConn(){
		Connection conn = null;
		try {
			
			Class.forName(BasisConstants.driverClassName).newInstance();
			conn = DriverManager.getConnection(BasisConstants.driverUrl,BasisConstants.account,BasisConstants.password); 
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return conn;
	}

	/* (非 Javadoc) 
	 * <p>Title: getTblTaskBasicConfig</p> 
	 * <p>Description: </p> 
	 * @return 
	 * @see cn.com.gome.task.dao.TaskInfoDAO#getTblTaskBasicConfig() 
	 */
	@Override
	public List<TblTaskBasicConfig> getTaskBasicConfig(){
		List<TblTaskBasicConfig> list = new CopyOnWriteArrayList<TblTaskBasicConfig>();
		ResultSet rs = null;
		Statement statement = null;
		Connection connection = null;
		try {
			connection = getConn();
			statement = connection.createStatement();
			String sql = "select id,task_name,run_rule,status,business_obj_name,parent_id,run_status,task_count,up_time,business_info,internet_name,server_ip" +
					" from tbl_task_basic_config WHERE parent_id=0 ";
			rs = statement.executeQuery(sql);
			TblTaskBasicConfig config = null;
			while(rs.next()){
				config = new TblTaskBasicConfig();
				config.setId(Integer.parseInt(rs.getString(1)));
				config.setTaskName(rs.getString(2));
				config.setRunRule(rs.getString(3));
				config.setStatus(Integer.parseInt(rs.getString(4)));
				config.setBusinessObjName(rs.getString(5));
				config.setParentId(Integer.parseInt(rs.getString(6)));
				config.setRunStatus(Integer.parseInt(rs.getString(7)));
				config.setTaskCount(rs.getInt(8));
				config.setUpTime(rs.getString(9));
				config.setBusinessInfo(rs.getString(10));
				config.setInternetName(rs.getString(11));
				config.setServerIp(rs.getString(12));
				list.add(config);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				if(rs!=null)
					rs.close();
				if(statement!=null)
					statement.close();
				if(connection!=null)
					connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return list;
	}

	@Override
	public List<TblAllStockInfo> getAllStockInfo() {
		List<TblAllStockInfo> list = new CopyOnWriteArrayList<TblAllStockInfo>();
		ResultSet rs = null;
		Statement statement = null;
		Connection connection = null;
		try {
			connection = getConn();
			statement = connection.createStatement();
			String sql = "select STOCK_CODE,EX,NAME,INDUSTRY from TBL_ALL_STOCK_INFO";
			rs = statement.executeQuery(sql);
			TblAllStockInfo config = null;
			while(rs.next()){
				config = new TblAllStockInfo();
				config.setStockCode(rs.getString(1));
				config.setEx(rs.getString(2));
				config.setName(rs.getString(3));
				config.setIndustry(rs.getString(4));
				list.add(config);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				if(rs!=null)
					rs.close();
				if(statement!=null)
					statement.close();
				if(connection!=null)
					connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return list;
	}

	/**
	 * 获取子任务 TBL_SITE_CHILD_TASK
	 * @return
	 */
	@Override
	public List<SiteChildTask> getSiteChildTask() {
		List<SiteChildTask> list = new ArrayList<SiteChildTask>();
		ResultSet rs = null;
		Statement statement = null;
		Connection connection = null;
		try {
			connection = getConn();
			statement = connection.createStatement();
			String sql = "select ID,SITE_TYPE,SEND_ADDRESS,TASK_ID,STATUS,RUN_COUNT,LATE_TIME from TBL_SITE_CHILD_TASK where STATUS=1";
			rs = statement.executeQuery(sql);
			SiteChildTask config = null;
			while(rs.next()){
				config = new SiteChildTask();
				config.setId(rs.getString(1));
				config.setSiteType(rs.getString(2));
				config.setSendAddress(rs.getString(3));
				config.setTaskId(rs.getString(4));
				config.setStatus(rs.getString(5));
				config.setRunCount(rs.getInt(6));
				config.setLateTime(rs.getInt(7));
				list.add(config);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				if(rs!=null)
					rs.close();
				if(statement!=null)
					statement.close();
				if(connection!=null)
					connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return list;
	}

	// TODO 死锁解锁任务
	@Override
	public List<DieTaskOpenConfig> getDieTaskOpenConfig() {
		List<DieTaskOpenConfig> list = new ArrayList<DieTaskOpenConfig>();
		ResultSet rs = null;
		Statement statement = null;
		Connection connection = null;
		try {
			connection = getConn();
			statement = connection.createStatement();
			String sql = "select ID,OPEN_ASK,SQLS,STATUS from TBL_DIE_TASK_OPEN_CONFIG where STATUS=1";
			rs = statement.executeQuery(sql);
			DieTaskOpenConfig config = null;
			while(rs.next()){
				config = new DieTaskOpenConfig();
				config.setId(rs.getString(1));
				config.setOpenAsk(rs.getString(2));
				config.setSqls(rs.getString(3));
				config.setStatus(rs.getString(4));
				list.add(config);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				if(rs!=null)
					rs.close();
				if(statement!=null)
					statement.close();
				if(connection!=null)
					connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return list;
	}

	@Override
	public boolean getDieTaskOpenConfigRun() {
		List<DieTaskOpenConfig> list = getDieTaskOpenConfig();

		if(list.size()>0){
			Statement statement = null;
			Connection connection = null;
			try {
				connection = getConn();
				statement = connection.createStatement();
				int count = 0;
				for(DieTaskOpenConfig config : list){
					count = statement.executeUpdate(config.getSqls());
					logger.info(config.getOpenAsk()+" 解锁成功，影响记录（"+count+"）条");
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}finally{
				try {
					if(statement!=null)
						statement.close();
					if(connection!=null)
						connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		return true;
	}
}
